// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_UI_TYPE } from "../Enum";

import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import SdkManager from "../manager/SdkManager";
import MainLayer from "./MainLayer";
import { privacyChannel } from "../Script/privacyChannel";
import { StaticInstance } from "../StaticInstance";


const { ccclass, property } = cc._decorator;

@ccclass
export default class AddTimeLayer extends BaseLayer {

    panel: cc.Node = null
    btnNext: cc.Node = null
    btnShare: cc.Node = null
    videoTip: cc.Node = null

    auto: Boolean = false
    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btnNext = cc.find('buttons/btn_next', this.panel)
        this.btnShare = cc.find('buttons/btn_share', this.panel)
        this.btnNext.on('click', this.onClickGet, this)
        this.btnShare.on('click', this.onClickClose, this)

    }
    show(): void {
        super.show()
        if (this.auto) {
            this.btnShare.getComponentInChildren(cc.Label).string = "返回桌面"
        } else {
            this.btnShare.getComponentInChildren(cc.Label).string = "关闭"
        }

        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
            case "vivo":
            case "vivo1":
            case "xm":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    privacyChannel.getChannel().showInter(true)
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }
                break
            case "ks":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    privacyChannel.getChannel().showInter(true)
                }
                break
        }
    }
    onDestroy() {
        this.btnNext.off('click', this.onClickGet, this)
        this.btnShare.off('click', this.onClickClose, this)
    }

    onEnable() {
        this.zoomIn(this.panel)
        SdkManager.instance.showInterstitialAd()
    }

    onDisable() { }

    onClickGet() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        privacyChannel.getChannel().showVideo(() => {
            this.node.parent.getChildByName("MainLayer").getComponent(MainLayer).refreshTime()
            this.node.active = false
        })
    }

    onClickClose() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        if (this.auto) {
            this.node.active = false
            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MAIN, false)
            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU)
        } else {
            this.node.active = false
        }

    }
}
